ige.controller('LoginController', ['$scope', '$state', 'AccountService', '$cookieStore', function ($scope, $state, AccountService, $cookieStore) {
    $scope.loadding = false;
    $scope.User = {
        Name: '',
        Password: '',
        rememberme: true
    };
    $scope.showHints = 0;

    var isSuccess = false;
    var resData = {};
    $scope.login = function () {
        $scope.loadding = true;
        AccountService.login($scope.User.Name, $scope.User.Password, $scope.User.rememberme, function (data) {
            isSuccess = true;
            resData = data;
            $cookieStore.put('access_token', resData.data.access_token);
        }).then(function () {
            if (resData.is_ok) {
                $state.go('MainNav');
            }

            $scope.loadding = false;
        });
    }
}]);

ige.controller('SystemUserController', ['$scope', 'AccountService', '$mdToast', '$state', 'EventBus', '$timeout', function ($scope, AccountService, $mdToast, $state, EventBus, $timeout) {
    var pageSkip = 0;
    var currentDeleteId = 0;
    var deleteArray = [];

    var timer;
    $scope.noNewData = false;
    EventBus.off("systemUser.keywordSearch");
    EventBus.on("systemUser.keywordSearch", function (event) {
        $timeout.cancel(timer);
        timer = $timeout(
            function () {
                pageSkip = 0;
                $scope.Users = [];
                $scope.filter = event.data;
                $scope.getUsers();
            }, 300
        );
    });

    $scope.$on(
        "$destroy",
        function (event) {
            $timeout.cancel(timer);
        }
    );

    $scope.Users = [];
    $scope.loadMore = false;
    $scope.loaddingListData = true;
    $scope.getUsers = function () {
        $scope.loaddingListData = true;
        AccountService.getUsers(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
            $scope.Users = $scope.Users.concat(data);
            if (data.length == 0) {
                $scope.noNewData = true;
            } else {
                $scope.noNewData = false;
            }
        }).then(function () {
            pageSkip += ige.systemSetting.defaultPageSize;
        }).then(function () {
            $scope.loaddingListData = false;
        });
    };

    $scope.loadMoreData = function () {
        $scope.loadMore = true;
        AccountService.getUsers(pageSkip, ige.systemSetting.defaultPageSize, $scope.filter, function (data) {
            $scope.Users = $scope.Users.concat(data);
            if (data.length == 0) {
                $scope.noNewData = true;
            } else {
                $scope.noNewData = false;
            }
        }).then(function () {
            pageSkip += ige.systemSetting.defaultPageSize;
        }).then(function () {
            $scope.loadMore = false;
        });
    };

    $scope.edit = function (id) {
        $state.go('MainNav.EditSystemUser', {id: id});
    };
    $scope.getUsers();
    $scope.itemHide = function (id) {
        return deleteArray.indexOf(id) > -1;
    };

    $scope.loadding = function (id) {
        return id == currentDeleteId;
    };

    var deleteSuccess = false;

    $scope.delete = function (id) {
        currentDeleteId = id;
        AccountService.deleteUser(id, function (data) {
            deleteSuccess = true;
        }).then(function () {
            if (deleteSuccess) {
                currentDeleteId = 0;
                deleteArray.push(id);
            }
            deleteSuccess = false;
        });
    }
}]);


ige.controller('AddSystemUserController', ['$scope', 'AccountService', '$mdToast', '$state', function ($scope, AccountService, $mdToast, $state) {
    var addSuccess = false;
    $scope.loadding = false;
    $scope.User = {
        Name: undefined,
        Description: undefined,
        Phone: undefined,
        Email: undefined,
        Password: undefined
    };

    $scope.addUser = function () {
        $scope.loadding = true;
        AccountService.addUser($scope.User, function (data) {
            addSuccess = true;
        }).then(function () {
            if (addSuccess) {
                $state.go('MainNav.SystemUser');
            } else {
            }
            $scope.loadding = false;
            addSuccess = false;
        });
    }
}]);


ige.controller('EditSystemUserController', ['$scope', 'AccountService', '$mdToast', '$state', '$stateParams', function ($scope, AccountService, $mdToast, $state, $stateParams) {
    $scope.loadding = false;
    var id = $stateParams.id;
    var editSuccess = false;
    $scope.User = {
        Id: id,
        Name: undefined,
        Description: undefined,
        Phone: undefined,
        Email: undefined,
        Password: undefined
    };

    AccountService.getUser(id, function (data) {
        $scope.User.Name = data.username;
        $scope.User.Description = data.description;
        $scope.User.Phone = data.phone;
        $scope.User.Email = data.email;
    });

    $scope.editUser = function () {
        $scope.loadding = true;
        AccountService.editUser($scope.User, function (data) {
            editSuccess = true;
        }).then(function () {
            if (editSuccess) {
                $state.go('MainNav.SystemUser');
            } else {
            }
            editSuccess = false;
            $scope.loadding = false;
        });
    }
}]);